/*
	main
*/

package main

import (
	"fmt"
	"geecache/geecache"
	"log"
	"net/http"
)

// 测试用db
var (
	testKey   = "test"
	testValue = "this is just a test"
)

var db = map[string]string{
	testKey: testValue,
}

// getter函数
func DBGetter(key string) ([]byte, error) {
	fmt.Println("start DBGetter")
	s, ok := db[key]
	if !ok {
		return nil, fmt.Errorf("key not found")
	}

	fmt.Println("end DBGetter")
	return []byte(s), nil
}

func main() {
	// 新建group
	geecache.NewGroup("testGroup", 0, geecache.GetterFunc(DBGetter))

	// 新建HTTPPool
	addr := "localhost:9999"
	httpPool := geecache.NewHTTPPool(addr)

	// ServeHTTP
	log.Println("start geecache HTTPServer at: ", addr)
	log.Fatal(http.ListenAndServe(addr, httpPool))
}
